<template>
  <div class="categoryBox">
    <div class="box">
      <div class="categoryItem" v-items @click="gotoUrl">
        <span class="iconfont icon-sousuo"></span>
      </div>
      <div
        :class="
          'categoryItem ' + (index === choolsIndex ? 'categoryAction' : '')
        "
        :ref="'categoryItem' + index"
        v-for="(item, index) in categoryList"
        :key="index"
        v-items="{ default: index == 0 ? true : false }"
        @click="choolsItem(index)"
      >
        <span>{{ item.category_name }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    categoryId() {
      return this.$store.getters.getCategoryId;
    }
  },
  data() {
    return {
      categoryList: false,
      choolsIndex: 0
    };
  },
  watch: {
    categoryList(newValue) {
      if (this.categoryId === false) {
        // 设置 vuex状态
        let categoryItem = newValue[0];
        categoryItem['index'] = 0;
        this.$store.commit('setCategoryId', categoryItem);
      }
    }
  },
  created() {
    this.categoryLoading();
  },
  methods: {
    gotoUrl() {
      this.$router.push({
        path: '/search'
      });
    },
    categoryLoading() {
      this.$api.categoryLoading(this);
    },
    choolsItem(index) {
      this.choolsIndex = index;
      // 设置 vuex状态
      let categoryItem = this.categoryList[index];
      categoryItem['index'] = index;
      this.$store.commit('setCategoryId', categoryItem);
    },
    inCategoryVitems() {
      this.$service.move(this.$refs['categoryItem0'][0]);
    }
  }
};
</script>

<style lang="less" scoped>
.categoryBox {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.box {
  width: 80%;
  background: #223c61;
  padding: 30px 4px;
  border-radius: 30px;
}
.categoryItem {
  font-size: 20px;
  line-height: 7vh;
  text-align: center;
  color: #fff;
  padding: 4px;
  width: 100%;
}
.categoryAction {
  border-radius: 10px;
  width: 110%;
  background: rgba(230, 0, 0, 1);
}
</style>
